<?php
//$moduleid = 30;
set_time_limit(0);
include '../../common.inc.php';
$dbMain   = new PDO('mysql:host=192.168.1.19;dbname=ixdcw', 'root', 'root');
$dbSource = new PDO('mysql:host=192.168.1.19;dbname=caiji', 'root', 'root');
$table    = 'video_qichezhijia';
$ds       = $dbSource->query('select max(ID) as num from ' . $table);
$fo       = $ds->fetchObject();
$maxId      = intval($fo->num);
if (!isset($id)) {
	$id = 0;
} else {
	if ($id >= $maxId) {
		die(date('Y-m-d H:i:s'));
	}
}
$sql     = "select * from {$table} where id > $id and yifa=0 limit 1";
$result  = $dbSource->query($sql);
$rs      = $result->fetchAll();
$setting = PCache::read('module-10.php');
/**
'ID' => string '1' (length=1)
'yicai' => string '1' (length=1)
'yifa' => string '0' (length=1)
'fenlei' => string '原创' (length=6)
'tupian' => string 'http://www0.autoimg.cn/video/carimg/2013/6/27/z_2013062709231235347.jpg' (length=71)
'title' => string '终极较量 7款百万级SUV全面对比第四期' (length=50)
'youku' => string 'XNTc2MTYyMDU2' (length=13)
'PageUrl' => string 'http://v.autohome.com.cn/v_8_22859.html' (length=39)
 */
foreach ($rs as $r) {
	$catid = handleVideoCategory($r['fenlei']);
	$content    = $dbMain->quote(Post::htmlEmbed($r['youku']));
	$title      = $r['title'];
	$addtime = time() - rand(0, 86400*120);
	//thumb
//	var_dump($r);exit();
	if(strlen(File::ext($r['tupian'])) < 10) {
		$orginal    = $r['tupian'];
		$remoteName = Picture::name(10, 1, File::ext($r['tupian']), $addtime);
		var_dump($remoteName);
		Picture::saveRemote($orginal, $remoteName, 'http://www.autohome.com.cn/');
		$url = Picture::url($remoteName);
		$thumb = Picture::thumb($url, $setting['thumb_width'], $setting['thumb_height']);
	} else {
		$thumb = '';
	}
	$edittime = time();
	$hits = rand(30, 5000);
	$insertMain = "insert into xd_video (catid,title, thumb, addtime, edittime, hits, status) values(
		$catid,
		'$title',
		'$thumb',
		'$addtime',
		'$edittime',
		'$hits',
		3
	)";

	$dbMain->query($insertMain);
	$insertId = $dbMain->lastInsertId();
	$insertData = "insert into xd_video_data values ($insertId, $content)";
	if (!$dbMain->query($insertData)) {
		var_dump($insertMain);
		exit($insertData);
	}
	var_dump($insertId);
	$id = $r['ID'];
	$dbSource->query(Sql::update($table, 'yifa=1', 'id='.$r['ID']));
//	exit($start);
}

function handleVideoCategory($name) {
	$array = array(
		'3271'=>'广告,卡车宣传广告,',
		'3273'=>'技术,卡车技术, ',
		'3274'=>'新车,',
		'3275'=>'试驾,事件,试车,',
		'3276'=>'评测,',
		'3277'=>'花边,',
		'3278'=>'改装,',
	);
	foreach($array as $key => $v) {
		if (strpos($v, $name.',') !== false) {
			return $key;
		}
	}
	return 0;
}

echo '<meta http-equiv="refresh" content="1; url=' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&debug=xddl" />';